<?php

namespace app\api\controller\user;

use think\Controller;

class Base extends Controller
{

    public function __construct()
    {
        parent::__construct();

        $this->UserModel = model('User.User');
    }

    // 登陆
    public function login()
    {
        if ($this->request->isPost()) {
            $email = $this->request->param('email', '');
            $password = $this->request->param('password', '');

            $userinfo = $this->UserModel->where('email', $email)->find();

            //当没有找到用户的情况下
            if (!$userinfo) {
                $this->error('用户不存在，请重新输入');
                exit;
            }

            //密码加密码   
            $password = md5($password . $userinfo['salt']);

            //判断密码
            if ($password != $userinfo['password']) {
                $this->error('密码输入有误，请重新输入');
                exit;
            }

            //组装数据
            $data = [
                'id' => $userinfo['id'],
                'email' => $userinfo['email'],
                'nickname' => $userinfo['nickname'],
                'avatar' => $userinfo['avatar_cdn'],
                'sex' => $userinfo['sex'],
                'province' => $userinfo['province'],
                'city' => $userinfo['city'],
                'district' => $userinfo['district'],
                'createtime' => $userinfo['createtime'],
                'content' => $userinfo['content'],
                'point' => $userinfo['point'],
                'vip' => $userinfo['vip'],
                'auth' => $userinfo['auth'],
                'cover' => $userinfo['cover_cdn'],
                'region' => $userinfo['region'],
            ];

            //跳转界面
            $this->success('登录成功', '/user/base/index', $data);
            exit;
        }
    }

    // 身份验证
    public function check()
    {
        if ($this->request->isPost()) {
            $id = $this->request->param('id', '');

            $userinfo = $this->UserModel->find($id);

            //当没有找到用户的情况下
            if (!$userinfo) {
                $this->error('用户不存在，请去注册');
                exit;
            }

            //组装数据
            $data = [
                'id' => $userinfo['id'],
                'email' => $userinfo['email'],
                'nickname' => $userinfo['nickname'],
                'avatar' => $userinfo['avatar_cdn'],
                'sex' => $userinfo['sex'],
                'province' => $userinfo['province'],
                'city' => $userinfo['city'],
                'district' => $userinfo['district'],
                'createtime' => $userinfo['createtime'],
                'content' => $userinfo['content'],
                'point' => $userinfo['point'],
                'vip' => $userinfo['vip'],
                'auth' => $userinfo['auth'],
                'cover' => $userinfo['cover_cdn'],
                'region' => $userinfo['region'],
            ];

            //跳转界面
            $this->success('登录成功', '/user/base/index', $data);
            exit;
        }
    }

    // 修改个人资料
    public function profile()
    {
        if ($this->request->isPost()) {

            $params = $this->request->param();
            $id = $this->request->param('id', 0);
            $password = $this->request->param('password', '');

            $userinfo = $this->UserModel->find($id);

            //当没有找到用户的情况下
            if (!$userinfo) {
                $this->error('用户不存在，请重新输入');
                exit;
            }

            $data = [
                'id' => $id,
                'nickname' => $params['nickname'],
                'sex' => $params['sex'],
            ];

            if(!empty($params['district'])){
                $data['district'] = $params['district'];
                $data['city'] = $params['city'];
                $data['province'] = $params['province'];
            }

            if(!empty($params['content'])){
                $data['content'] = $params['content'];
            }
         
            // 修改密码
            if ($password) {
                $data['password'] = md5($params['password'] . $userinfo['salt']);
            }

            if (isset($_FILES['avatar']) && $_FILES['avatar']['size'] > 0) {

                // 头像上传
                $avatarUrl = upload('avatar');

                if ($avatarUrl === FALSE) {
                    $this->error('修改失败');
                    exit;
                }

                $data['avatar'] = "uploads/" . $avatarUrl;
            }

            if (isset($_FILES['cover']) && $_FILES['cover']['size'] > 0) {

                // 封面上传
                $coverUrl = upload('cover');

                if ($coverUrl === FALSE) {
                    $this->error('修改失败');
                    exit;
                }

                $data['cover'] = "uploads/" . $coverUrl;
            }
          
            $result = $this->UserModel->isUpdate()->save($data);

            //当没有找到用户的情况下
            if ($result === FALSE) {

                //上传上来的图片删除了
                @is_file($data['cover']) && @unlink($data['cover']);
                @is_file($data['avatar']) && @unlink($data['avatar']);

                $this->error('修改失败');
                exit;
            } else {

                //修改成功后，将原来的图片删除
                //先判断是否有选择新的图片
                if (@is_file($data['avatar'])) {
                    //修改成功后，将原来的图片删除
                    $avatar = ltrim($userinfo['avatar'], "/");
                    @is_file($avatar) && @unlink($avatar);
                }

                //先判断是否有选择新的图片
                if (@is_file($data['cover'])) {
                    //修改成功后，将原来的图片删除
                    $cover = ltrim($userinfo['cover'], "/");
                    @is_file($cover) && @unlink($cover);
                }
            }

            $user = $this->UserModel->find($id);

            //组装数据
            $res = [
                'id' => $user['id'],
                'email' => $user['email'],
                'nickname' => $user['nickname'],
                'avatar' => $user['avatar_cdn'],
                'sex' => $user['sex'],
                'province' => $user['province'],
                'city' => $user['city'],
                'district' => $user['district'],
                'createtime' => $user['createtime'],
                'content' => $user['content'],
                'point' => $user['point'],
                'vip' => $user['vip'],
                'auth' => $user['auth'],
                'cover' => $user['cover_cdn'],
                'region' => $user['region'],
            ];

            //跳转界面
            $this->success('修改资料成功', '/user/base/index', $res);
            exit;
        }
    }

    public function register()
    {
        if ($this->request->isPost()) {
            $email = $this->request->param('email', '');
            $password = $this->request->param('password', '');

            $userinfo = $this->UserModel->where('email', $email)->find();

            //当没有找到用户的情况下
            if ($userinfo) {
                $this->error('用户存在，请重新输入邮箱');
                exit;
            }

            $salt = randstr(8);

            $password = md5($password.$salt);

            $data = [
                'email' =>$email,
                'password' =>$password,
                'salt' =>$salt 
            ];

            $result = $this->UserModel->save($data);

            if($result === FALSE)
            {
                $this->error($this->UserModel->getError());
            } else
            {
                $this->success('注册成功', '');
                exit;
            }
        }
    }
}